﻿using System;
using System.Windows.Forms;

namespace Operacional
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtNome.Text))
            {
                Program.entrada.lblStatus.Text = "Utilizador: " + txtNome.Text;
                Program.entrada.LockTool.Image = Properties.Resources.UNLOCKED;

                FormClosing -= new FormClosingEventHandler(frmLogin_FormClosing);
                Close();
            }
            else
            {
                txtCodigo.Focus();
                MessageBox.Show("Sem utilizador definido!", "Erro");
            }
        }

        private void txtCodigo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) btnLogin_Click(sender, e);
        }

        private void txtCodigo_TextChanged(object sender, EventArgs e)
        {
            frmEntrada.dvUsers.RowFilter = "Codigo ='" + txtCodigo.Text + "'";
            if (frmEntrada.dvUsers.Count > 0) txtNome.Text = frmEntrada.dvUsers[0].Row[1].ToString();
            else txtNome.Clear();

            if (string.IsNullOrEmpty(txtCodigo.Text)) txtNome.Clear();
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            FormClosing -= new FormClosingEventHandler(frmLogin_FormClosing);
            Close();
            Application.Exit();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            TopMost = true;
            Program.entrada.LockTool.Image = Properties.Resources.LOCK;
            frmEntrada.tmrLogin.Enabled = false;

            bool BtCancelar = true;
            foreach (Form Frm in Application.OpenForms)
            {
                if (Frm.Name != "frmEntrada" && Frm.Name != "frmLogin")
                    BtCancelar = false;
            }
            btCancelar.Enabled = BtCancelar;
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            base.OnClosing(e);
        }

        void LblVirtualkeyboardClick(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("OSK.exe");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
